﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Cn.Edwon.Web.Support;
using Cn.Cellula.Client;
using Cn.Cellula.Biz;
using Cn.Cellula.Biz.Port;
using Cn.Edwon.Spring;
using Cn.Cellula.Model.Calendar;
using Cn.Edwon.Web.Validation;
using Cn.Edwon.Web;
using System.Reflection;

namespace Cn.Cellula.Website.Admin.Calendar {
    public partial class EditToDo : PageSupport {
        String eoKey = null;
        ICalendarBiz biz = BizFactory.Get<ICalendarBiz>();
        DateTime CurDt = DateTime.Now;
        protected void Page_Load(object sender, EventArgs e) {
            if (!String.IsNullOrEmpty(Request.QueryString["dt"])) {
                try {
                    CurDt = Convert.ToDateTime(Request.QueryString["dt"]);
                } catch(Exception ex) {
                    log.Error("EditTodo:PageLoad:Bad dt param:" + ex.Message);
                    CurDt = DateTime.Now;
                }
            } 
            eoKey = Request.QueryString["id"];
            if (!Page.IsPostBack) {
                InitForm();
                FillForm();
            }
        }

        private void InitForm() {
            ClientBiz.BindCodeList(ref this.ddl_TodoType, CodeTypeDef.Todotype.Key, true, "");
            ClientBiz.BindCodeList(ref this.rbl_TodoLevel, CodeTypeDef.Todolevel.Key, true, "");
            if(this.rbl_TodoLevel.Items.Count>0)
                this.rbl_TodoLevel.Items[0].Selected = true;

            this.txt_Occur.Text = CurDt.ToString("yyyy-MM-dd");
        }

        private void FillForm() {
            if (!String.IsNullOrEmpty(eoKey)) {
                VCalendarTodoEO eo = biz.LoadCalendarTodo(this.OwnerKey, this.Actor.ID, eoKey);
                if (eo == null || eo.IsLocked==1) {
                    this.btn_save.Visible = false;
                    this.btn_save.Text = "已月结锁定";
                    return;
                }
                this.rbl_TodoLevel.SelectedValue = eo.TodoLevel;
                this.ddl_TodoType.SelectedValue = eo.TodoType;
                this.txt_Content.Text = eo.Content;
                DateTime _dt;
                if (eo.StartToReal != null) {
                    _dt = Convert.ToDateTime(eo.StartToReal);
                    this.txt_StartHour.Text = _dt.Hour + "";
                    this.txt_StartMinute.Text = _dt.Minute + "";
                }
                if (eo.EndToReal != null) {
                    _dt = Convert.ToDateTime(eo.EndToReal);
                    this.txt_EndHour.Text = _dt.Hour + "";
                    this.txt_EndMinute.Text = _dt.Minute + "";
                }
                this.txt_TodoName.Text = eo.TodoTitle;
                this.hid_AssignTo.Value = eo.AssignToUser;
                this.txt_AssignTo.Text = eo.AssignToUserName;
                this.hid_Members.Value = eo.MembersKey;
                this.txt_Members.Text = eo.MembersName;
                this.txt_Item.Text = eo.RefItemName;
                this.hid_Item.Value = eo.RefItemKey;
                this.txt_CostHours.Text = eo.CostHours + "";
                this.txt_Occur.Text = eo.OccurDt.ToString("yyyy-MM-dd");
            } else {
                this.txt_StartHour.Text = DateTime.Now.Hour + "";
                this.txt_StartMinute.Text = DateTime.Now.Minute + "";
            }
        }
        protected void btn_save_Click(object sender, EventArgs e) {
            Boolean bk = QuickValidator.Execute(Request, this.txt_TodoName.Parent);
            if (!bk) {
                SiteInfo.Send(Page, TxtDef.ERROR, QuickValidator.Message);
                return;
            } 
            TCalendarTodoEO eo = null;
            if (String.IsNullOrEmpty(eoKey)) { //create
                eo = new TCalendarTodoEO();
                eo.Content = this.txt_Content.Text;
                eo.CreateDt = DateTime.Now;
                eo.IsPrivate = Convert.ToInt32(this.rbl_Private.SelectedValue);
                eo.LastDt = DateTime.Now;
                eo.OwnerKey = this.OwnerKey;
                try {
                    eo.OccurDt = Convert.ToDateTime(this.txt_Occur.Text).AddHours(9);
                } catch {
                    return;
                }
                if (biz.IfOccurDtChecked(eo.OccurDt)) { 
                    SiteInfo.Send(Page,"INFO",eo.OccurDt.ToString("yyyy年MM月")+"已结，无法录入该月数据");
                    return;
                }
                try {
                    eo.StartToPlan = eo.StartToReal = Convert.ToDateTime(eo.OccurDt.ToString("yyyy-MM-dd ") + this.txt_StartHour.Text + ":" + this.txt_StartMinute.Text + ":0");
                } catch(Exception ex) {
                    log.Error("EditTodo:StartTime:" + ex.Message);
                    eo.StartToPlan = eo.StartToReal = Convert.ToDateTime(eo.OccurDt.ToString("yyyy-MM-dd ") + DateTime.Now.ToString("HH:mm:ss"));
                }
                try {
                    eo.EndToPlan = eo.EndToReal = Convert.ToDateTime(eo.OccurDt.ToString("yyyy-MM-dd ") + this.txt_EndHour.Text + ":" + this.txt_EndMinute.Text + ":0");
                } catch(Exception ex) {
                    log.Error("EditTodo:EndTime:" + ex.Message);
                    eo.EndToPlan = eo.EndToReal = null;
                }
                eo.IfClose = (eo.EndToReal != null) ? 1 : 0;
                eo.RefItemKey = this.hid_Item.Value;
                eo.TodoLevel = this.rbl_TodoLevel.SelectedValue;
                eo.TodoTitle = this.txt_TodoName.Text;
                eo.TodoType = this.ddl_TodoType.SelectedValue;
                eo.Operator = this.Actor.ID;
                if (!String.IsNullOrEmpty(this.hid_AssignTo.Value)) {
                    eo.AssignToUser = this.hid_AssignTo.Value;
                } else {
                    eo.AssignToUser = this.Actor.ID;
                }
                try {
                    eo.CostHours = Convert.ToDecimal(this.txt_CostHours.Text);
                } catch { }
                
                Object newKey = biz.Save<TCalendarTodoEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("待办事项", eo.TodoTitle, this.Actor, Request);

                biz.SaveOrUpdateMembers(newKey.ToString(), this.hid_Members.Value);
                Response.Redirect("Default.aspx?mode=1&dt=" + eo.OccurDt.ToString("yyyy-MM-dd"));
            } else {
                eo = biz.Load<TCalendarTodoEO>(eoKey);
                if (eo.IsLocked == 1) return;
                eo.Content = this.txt_Content.Text;
                eo.IsPrivate = Convert.ToInt32(this.rbl_Private.SelectedValue);
                eo.LastDt = DateTime.Now;
                try {
                    eo.OccurDt = Convert.ToDateTime(this.txt_Occur.Text).AddHours(9);
                } catch {
                    return;
                }
                try {
                    eo.StartToPlan = eo.StartToReal = Convert.ToDateTime(eo.OccurDt.ToString("yyyy-MM-dd ") + this.txt_StartHour.Text + ":" + this.txt_StartMinute.Text + ":0");
                } catch (Exception ex) {
                    log.Error("EditTodo:Update:StartTime:" + ex.Message);
                }
                try {
                    eo.EndToPlan = eo.EndToReal = Convert.ToDateTime(eo.OccurDt.ToString("yyyy-MM-dd ") + this.txt_EndHour.Text + ":" + this.txt_EndMinute.Text + ":0");
                } catch (Exception ex) {
                    log.Error("EditTodo:Update:EndTime:" + ex.Message);
                }
                eo.IfClose = (eo.EndToReal != null) ? 1 : 0;
                eo.RefItemKey = this.hid_Item.Value;
                eo.TodoLevel = this.rbl_TodoLevel.SelectedValue;
                eo.TodoTitle = this.txt_TodoName.Text;
                eo.TodoType = this.ddl_TodoType.SelectedValue;
                try {
                    eo.CostHours = Convert.ToDecimal(this.txt_CostHours.Text);
                } catch {
                    eo.CostHours = 0;
                }

                biz.Update<TCalendarTodoEO>(eo);
                BizFactory.Get<ILogBiz>().LogInsert("待办事项", eo.TodoTitle, this.Actor, Request);
                biz.SaveOrUpdateMembers(eoKey, this.hid_Members.Value);
                Response.Redirect("Default.aspx?mode=1&dt="+eo.OccurDt.ToString("yyyy-MM-dd"));
            }
        }
    }
}